namespace IAM.Interface.Model
{
    using IAM.Interface.Common;
    using IAM.Interface.Model.Equals;
    using IAM.Interface.Model.Interface;
    using System;
    using System.Collections;
    using System.Reflection;
    using System.Text;
    using System.Xml;

    public class ACISessions : AbstractConvertObject, IConvertXml
    {
        private ArrayList _acisessions = null;
        private ArrayList _compareresultlist = null;

        public ACISessions()
        {
            this._acisessions = new ArrayList();
        }

        public override object ConvertToObject()
        {
            foreach (XmlNode xn in this.CurrentXmlNode.ChildNodes)
            {
                ACISession acisession = new ACISession();
                acisession.CurrentXmlNode = xn;
                acisession = (ACISession) acisession.ConvertToObject();
                this.Items.Add(acisession);
            }
            return this;
        }

        public override bool Equals(object obj)
        {
            ACISessions acisessions = null;
            try
            {
                acisessions = (ACISessions) obj;
            }
            catch
            {
                return false;
            }
            if (this.Items.Count != acisessions.Items.Count)
            {
                return false;
            }
            bool boolforeach = true;
            foreach (object rudeobj in this.Items)
            {
                boolforeach = false;
                foreach (object aimobj in acisessions.Items)
                {
                    if (((ACISession) rudeobj).Equals(aimobj))
                    {
                        boolforeach = true;
                        break;
                    }
                }
                if (!boolforeach)
                {
                    return boolforeach;
                }
            }
            return boolforeach;
        }

        public ArrayList GetCompareResultList(ACISessions source, ACISessions obj)
        {
            if (source == null)
            {
                source = new ACISessions();
            }
            if (obj == null)
            {
                obj = new ACISessions();
            }
            CompareResult cr = null;
            this._compareresultlist = new ArrayList();
            XmlDocument xdsource = new XmlDocument();
            xdsource.LoadXml(source.XML);
            XmlDocument xdobj = new XmlDocument();
            xdobj.LoadXml(obj.XML);
            this._compareresultlist = new ArrayList();
            foreach (object oneofsourceitems in source.Items)
            {
                ACISession sourceACIS = (ACISession) oneofsourceitems;
                string key = "";
                if (sourceACIS.Items.Count > 0)
                {
                    key = EnDeCoding.Encode(((CheckedItem) sourceACIS.Items[0]).ACItemID);
                }
                XmlNode xnobject = xdobj.SelectSingleNode("//CheckedItem[@ACItemID='" + key + "']");
                if (xnobject != null)
                {
                    xnobject = xnobject.ParentNode;
                }
                if (xnobject == null)
                {
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = true;
                    cr.InObject = false;
                    cr.ContenctEquation = false;
                    this.CompareresultList.Add(cr);
                }
                else
                {
                    ACISession objectACIS = new ACISession();
                    objectACIS.CurrentXmlNode = xnobject;
                    objectACIS.ConvertToObject();
                    if (!sourceACIS.Equals(objectACIS))
                    {
                        cr = new CompareResult();
                        cr.ID = key;
                        cr.InSource = true;
                        cr.InObject = true;
                        cr.ContenctEquation = false;
                        this.CompareresultList.Add(cr);
                    }
                }
            }
            foreach (object oneofobj in obj.Items)
            {
                ACISession objectACI = (ACISession) oneofobj;
                string key = "";
                if (objectACI.Items.Count > 0)
                {
                    key = EnDeCoding.Encode(((CheckedItem) objectACI.Items[0]).ACItemID);
                }
                XmlNode xnsource = xdsource.SelectSingleNode("//CheckedItem[@ACItemID='" + key + "']");
                if (xnsource != null)
                {
                    xnsource = xnsource.ParentNode;
                }
                if (xnsource == null)
                {
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = false;
                    cr.InObject = true;
                    cr.ContenctEquation = false;
                    this.CompareresultList.Add(cr);
                    continue;
                }
                ACISession sourACIS = new ACISession();
                sourACIS.CurrentXmlNode = xnsource;
                sourACIS.ConvertToObject();
                if (!objectACI.Equals(sourACIS))
                {
                    foreach (object objresult in this.CompareresultList)
                    {
                        if (((CompareResult) objresult).ID == key)
                        {
                            this.CompareresultList.Remove(objresult);
                            break;
                        }
                    }
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = true;
                    cr.InObject = true;
                    cr.ContenctEquation = false;
                    this.CompareresultList.Add(cr);
                }
            }
            if (this.CompareresultList.Count > 0)
            {
                return this.CompareresultList;
            }
            return null;
        }

        public override int GetHashCode()
        {
            int result = 1;
            foreach (PropertyInfo onofpi in base.GetType().GetProperties())
            {
                result += onofpi.GetHashCode();
            }
            return result;
        }

        public ArrayList CompareresultList
        {
            get
            {
                return this._compareresultlist;
            }
        }

        public int Count
        {
            get
            {
                return this.Items.Count;
            }
        }

        public ArrayList Items
        {
            get
            {
                return this._acisessions;
            }
            set
            {
                this._acisessions = value;
            }
        }

        public string XML
        {
            get
            {
                return (this.XMLBoday + this.XMLFinis);
            }
        }

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append("<ACISessions");
                _xml.Append(" Count=\"" + this.Count + "\"");
                _xml.Append(" >");
                foreach (object aciSession in this.Items)
                {
                    _xml.Append(((ACISession) aciSession).XML);
                }
                return _xml.ToString();
            }
        }

        public string XMLFinis
        {
            get
            {
                return "</ACISessions>";
            }
        }
    }
}

